# Disable in-source builds to prevent source tree corruption.
if(" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
  message(FATAL_ERROR "
FATAL: In-source builds are not allowed.
       You should create a separate directory for build files.
")
endif()

cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
project(cmakeframework LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
endif()
message(STATUS "Build type set to ${CMAKE_BUILD_TYPE}")



set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects)

set(STAGED_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/stage)
message(STATUS "${PROJECT_NAME} staged install: ${STAGED_INSTALL_PREFIX}")

add_subdirectory(external/upstream)

include(ExternalProject)
ExternalProject_Add(src
  DEPENDS
    glog_external
    gflags_external
  SOURCE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/src
  CMAKE_ARGS
    -DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
    -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
    -DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}
    -Dglog_DIR=${glog_DIR}
    -Dgflags_DIR=${gflags_DIR}
  CMAKE_CACHE_ARGS
    -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
    -DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}
  BUILD_ALWAYS
    1
  # INSTALL_COMMAND
  #   ""
  )

###############################################################################################################################
###      FOR TEST
###############################################################################################################################

option(ENABLE_UNIT_TESTS "Enable unit tests" ON)
message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}")
if(ENABLE_UNIT_TESTS)
ExternalProject_Add(test
  DEPENDS
    src
  SOURCE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/tests
  CMAKE_ARGS
    -DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
    -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
    -DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}
    -Dglog_DIR=${glog_DIR}
    -Dgflags_DIR=${gflags_DIR}
  CMAKE_CACHE_ARGS
    -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
    -DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}
  BUILD_ALWAYS
    1
  # INSTALL_COMMAND
  #   ""
  )
endif()

###############################################################################################################################
###      FOR PACKAGE
###############################################################################################################################
option(ENABLE_PACKAGE "Enable package " OFF)
message(STATUS "Enable package: ${ENABLE_PACKAGE}")
if(ENABLE_PACKAGE)
 # 设置输出路径
 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/lib)
 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin)
 
 # 常见依赖库
 find_package(PkgConfig REQUIRED)
 pkg_check_modules(libglog REQUIRED IMPORTED_TARGET libglog)
 pkg_check_modules(libzmq  REQUIRED IMPORTED_TARGET libzmq)
 pkg_check_modules(pugixml REQUIRED IMPORTED_TARGET pugixml)
 pkg_check_modules(openssl REQUIRED IMPORTED_TARGET openssl)
 pkg_check_modules(sqlite3 REQUIRED IMPORTED_TARGET sqlite3)
 find_library (HANDY_LIBS handy_s)
 
 # 子项目。
 add_subdirectory("extern")
 add_subdirectory("app")
 add_subdirectory("capture")
 
 ##---------------- 以下为用CPack打包成deb包的内容 ----------------##
 # 指定deb包的版本号
 set(_VERSION_MAJOR 1)
 set(_VERSION_MINOR 0)
 set(_VERSION_PATCH 1)
 set(CPACK_PACKAGE_VERSION_MAJOR "${_VERSION_MAJOR}")
 set(CPACK_PACKAGE_VERSION_MINOR "${_VERSION_MINOR}")
 set(CPACK_PACKAGE_VERSION_PATCH "${_VERSION_PATCH}")
 set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/${PROJECT_NAME}")
 
 # 指定CPack生成的是deb包（还可以是其他包如 <.7z> <.zip> 等）
 set(CPACK_GENERATOR "DEB")
 # # 指定包名
 set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
 # # 指定程序名，就是程序安装后的名字
 # set(CPACK_DEBIAN_PACKAGE_NAME "southi1_app")
 # 指定运行的计算机架构
 set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "arm64")
 # 指定所需的依赖
 # set(CPACK_DEBIAN_PACKAGE_DEPENDS "libgrpc++")
 # 设置包作者的联系方式
 set(CPACK_PACKAGE_CONTACT "1539731480@qq.com")
 # 设置维护人
 set(CPACK_DEBIAN_PACKAGE_MAINTAINER "xudongxu")
 
 # 设置每个分组打包成一个 DEB 包
 set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
 # 设置支持 COMPONENT
 # set(CPACK_DEB_COMPONENT_INSTALL ON)
 
 include(CPack)
 
 # 设置安装前执行的脚本文件 preinst
 set(CPACK_DEB_PRE_INSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/package/preinst)
 # 设置卸载前执行的脚本文件 prerm
 set(CPACK_DEB_PRE_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/package/prerm)
 # 设置安装后执行的脚本文件 postinst
 set(CPACK_DEB_POST_INSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/package/postinst)
 # 设置卸载后执行的脚本文件 postrm
 set(CPACK_DEB_POST_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/package/postrm)
 
 # 指定需要安装的文件
#  set(INSTALLPATH "/opt/southi1/app")
#  set(BUILDENV "/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/usr/local")
 
#  INSTALL(TARGETS app capture rtppusher hkcameratoolkit
#          RUNTIME DESTINATION ${INSTALLPATH}/bin
#          LIBRARY DESTINATION ${INSTALLPATH}/lib
#          ARCHIVE DESTINATION ${INSTALLPATH}/lib 
#          )
 
#  INSTALL(PROGRAMS ${CMAKE_SOURCE_DIR}/package/run.sh DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(PROGRAMS ${BUILDENV}/bin/ffmpeg DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(PROGRAMS ${BUILDENV}/bin/sqlite3 DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(DIRECTORY ${BUILDENV}/lib/ DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/config DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/data DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/scripts   DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/data DESTINATION ${INSTALLPATH}/bin)
#  INSTALL(DIRECTORY ${BUILDENV}/tegra DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(DIRECTORY ${BUILDENV}/cuda-9.0/lib64 DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(DIRECTORY ${BUILDENV}/cuda-9.0/targets/aarch64-linux/lib/ DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(DIRECTORY ${BUILDENV}/opencv-3.4.1/lib/ DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdpart/detect/lib/ DESTINATION ${INSTALLPATH}/lib)
#  INSTALL(FILES ${BUILDENV}/../../libc/lib/libstdc++.so.6 DESTINATION ${INSTALLPATH}/lib)

endif()